package graphics.view2Danimated;

import model.Square;
import graphics.interfaces.IDrawable;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Board2D implements IDrawable {
	
	private Sprite [][]sprite;
	private Sprite spriteSelectedSquare;
	private Square selectedSquare;
	
	public void LoadContent(){
		Texture wbTexture = new Texture(Gdx.files.internal("data/images/bw_tile.png"));
		TextureRegion rWhite = new TextureRegion(wbTexture, 0, 0, 128, 128);
		TextureRegion rBlack = new TextureRegion(wbTexture, 128, 0, 128, 128);
		
		sprite = new Sprite[8][8];
		// main board squares sprites
		for(int x = 0; x < 8; x ++){
			for(int y = 0; y < 8; y ++){
				if ((y + x) % 2 != 0) {
					sprite[x][y] = new Sprite(rBlack, 0, 0, 60, 60);
				} else {
					sprite[x][y] = new Sprite(rWhite, 0, 0, 60, 60);
				}
				sprite[x][y].setPosition(x*60, y*60);
			}	
		}
		// selected square sprite
		spriteSelectedSquare = new Sprite(rBlack, 0, 0, 60, 60);
		spriteSelectedSquare.setColor(1f, 0f, 0f, 0.5f);
		selectedSquare = Square.OUT_OF_RANGE;
	}
	
	public void Draw(SpriteBatch spriteBatch){
		// Draw board tiles
		for(int x = 0; x < 8; x ++){
			for(int y = 0; y < 8; y ++){
				sprite[x][y].draw(spriteBatch);
			}
		}
		// Draw selected square tile
		if(selectedSquare!=Square.OUT_OF_RANGE){
			int x = selectedSquare.getCol();
			int y = selectedSquare.getRow();
			spriteSelectedSquare.setPosition(60 * x, 60 * y);
			spriteSelectedSquare.draw(spriteBatch);
		}
	}
	
	public void SelectSquare(Square square) {
		selectedSquare = square;
	}
	
	public void UnselectSquare() {
		selectedSquare = Square.OUT_OF_RANGE;
	}
}
